//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"

#define WIDETH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark - 课件
#if 1
    {
        //    程序的组成构造
        //    1.main。m 主要实现了程序的正常运行
        //    2. Appdelegate 程序的执行者，签订了UIAplicationDelegate(协议方法可选)
        //     注意：AppDelegate就是一个普通的类,只不过签订协议, 才有了这么多方法
        //    3. ViewController视图控制器 主要负责视图的管理 看的见的、摸得到的
        //    4. Main.sb（视图化管理） LaunchScreen.sb(主要负责启动页) 可视化管理（Xcode 7以前 LaunchScreen.xib）
        //    5. Assets.xcassets 主要用来管理图片素材 （image.xcassets 在Xcode 7以前）
        //     6. Info.plist (工程配置文件)
        
        //    模拟器
        //    command + 1~5 改变模拟器的大小
        //    command +◀️/ ▶️  改变横屏和竖屏
        //    IOS 概述
        //    iOS 错误的写法 iOS
        
        
        //  1. UIWindow （程序中只有一个主要的window）
        //    1.>创建window之前要关闭程序的可视化的入口, main.sb
        //    2.>创建主window对象
        //    3.创建主window， 使其大小与屏幕一样
        //    UIScreen 系统屏幕类
        
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        //    UIColor 给你创建的窗口添加颜色 系统的颜色类
        _window.backgroundColor = [UIColor redColor];
        //    让window显示, 成为主窗口 并 显示
        ViewController *root = [[ViewController alloc] init];
        //    4. 设置window的主窗口
        //    Xcode 7 以后, 必须设置rootViewController, 否则, 在该方法结束时, 会崩溃
        self.window.rootViewController = root;
        [_window makeKeyAndVisible];
        
        // 3. UIWindow 继承于 UIview
        //       2. UIView(基本上与NSObject相似, 是UI阶段的根类，大部分UI控件继承与UIview)
        //    学习新类
        //    1>首先要知道新类包含哪些初始化方法
        //    2> 包含哪些行为（方法）和特征（属性）
        
        //    创建UIview
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,299, 299)];
        //    设置背景色
        view.backgroundColor = [UIColor greenColor];
        //    添加到window
        [_window addSubview:view];
        //    遇到uiview的子类都可以以上方式去创建和显示
        //    uiview 在屏幕中显示为矩形
        
        //    iOS系统坐标系
        //    1. 与数学中的坐标系不同， y轴的正方向是向下的
        //    2. 原点是屏幕的左上角
        //  frame bounds center (UIView 的属性)
        //    1. frame （x，y，wideth，height） 视图在其父视图的位置
        //    2. bounds （x，y，wideth，height）(自己视图本身的坐标系)
        //    3. center (x, y) （中心点）改变中心点
        view.frame = CGRectMake(90, 90, 100, 100);
        //    view.center = CGPointMake(200, 200);
        
        
        //    bounds 对视图的影响
        //    bounds 的 x，y 影响姿子视图的frame, 改变坐标系
        
        //    bounds x. y 修改的本身的坐标系，影响他的子视图
        //    bounds wideth , height修改的是本身的大小, 会以中心点收缩/扩张
        //    bounds 的改变, 不会改变该视图的位置
        //    bounds的x,y影响子视图的frame. 改变坐标系
        //    CGRect 和 CGPoint
        NSLog(@"改变bounds前%@", NSStringFromCGRect(view.frame));
        NSLog(@"改变前的center%@",NSStringFromCGPoint(view.center));
        view.bounds = CGRectMake(0, 0, 120, 100);
        NSLog(@"改变后的center%@",
              NSStringFromCGPoint(view.center));
        NSLog(@"改变后的坐标%@", NSStringFromCGRect(view.frame));
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        view2.backgroundColor = [UIColor yellowColor];
        [view addSubview:view2];
        //    view.HX_height = 100;
        
        //    view.frame 是getter方法
        //    frame.size 结构体 是setter方法
        
        //    改变绿色view的大小, width 变成 100
        //    OC和C的内容结合, 是一个常量
        //    view.frame.size.width = 100
        //    CGRect frame = view.bounds;
        //    frame.size.width = 100;
        //    view.frame = frame;
        view.HX_width = 100;
        
    }
#endif
    
    
#pragma mark - UIView的基本属性
#if 1
    {
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        //    UIColor 给你创建的窗口添加颜色 系统的颜色类
        _window.backgroundColor = COLORAUTO;
        //    让window显示, 成为主窗口 并 显示
        ViewController *root = [[ViewController alloc] init];
        //    4. 设置window的主窗口
        //    Xcode 7 以后, 必须设置rootViewController, 否则, 在该方法结束时, 会崩溃
        self.window.rootViewController = root;
        [_window makeKeyAndVisible];
        UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        //        添加
        [_window addSubview:testView];
        //        移除
        [_window removeFromSuperview];
        //       1. 背景颜色
        testView.backgroundColor = [UIColor cyanColor];
        //        2. 透明度 alpha 0~1 默认值 1.0
        testView.alpha = 0.25;
        //        3. tag 标记 （编号）默认是0
        testView.tag = 1000;
        //        根据tag获取到对应的view __kindof
        //   UIButton *btn = [testView viewWithTag:1000];
        //      4. _window.subView 子视图们
        
        UIView *view = [_window.subviews objectAtIndex:0];
                NSLog(@"%@", view);
        //        5.superView 父视图
        //        testView.superview
        //        6hidden 隐藏和显示  隐藏式yes 显示是NO 默认是no
        testView.hidden = YES;
        
        //       视图层级
        UIView *first = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
        first.backgroundColor = [UIColor yellowColor];
        UIView *second = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
        second.backgroundColor = [UIColor whiteColor];
        UIView *last = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
        last.backgroundColor = [UIColor blackColor];
        // addSubview
        [_window addSubview:first];
        // insertSubview:atIndex:(插入视图, 到制定的下表)
        [_window insertSubview:second atIndex:3];
        // insertSubview:aboveSubview(插入视图, 到制定视图之上)
        [_window insertSubview:last aboveSubview:second];
        // insertSubview:belowSubview（插入视图,到制定视图之下）
        [_window insertSubview:last belowSubview:second];
        
//        NSLog(@"%@", _window.subviews);
        
        //   UILabel(负责用来显示文本的)
        //    如果类没有自己的初始化方法, 则使用父类的
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(90, 300, 200, 100)];
        lable.backgroundColor = [UIColor yellowColor];
        [_window addSubview:lable];
//        1. text 文本
        lable.text = @"我是张合蔚， 我是逗比,bibibibibibi";
//        2. textColor 文本颜色
        lable.textColor = [UIColor purpleColor];
//        3。font
        lable.font = [UIFont systemFontOfSize:17];
//        4. textAlignment 对齐方式 (默认对齐方式 left)
//        left right center
        lable.textAlignment = NSTextAlignmentCenter;
//        5.numberOfLines 默认为1行
//        不确定行数时给0
//        6. lineBreakMode 换行模式, ...的位置
        lable.lineBreakMode = 1;
        lable.numberOfLines = 0;
//        7. 阴影 shadowColor
        lable.shadowColor = [UIColor blackColor];
        lable.shadowOffset = CGSizeMake(2, 1);
        
//        UIbutton (用来点的)
//      UIButtonTypeSystem 系统的按钮类型
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(10, 400, 100, 100);
        button.backgroundColor = [UIColor orangeColor];
//        button添加文字
        [button setTitle:@"请点击啊喂" forState:UIControlStateNormal];
//        修改button的文字颜色
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        button.layer.cornerRadius = 50;
        [_window addSubview:button];
//        给button添加点击事件
        [button addTarget:self action:@selector(buttonWithChilk:) forControlEvents:UIControlEventTouchUpInside];
//        UITextFileld
        UITextField *filed = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        filed.backgroundColor = [UIColor whiteColor];
        filed.HX_x = 100;
        filed.HX_y = 200;
        filed.HX_width = 200;
        filed.HX_height = 50;
//        1. text 给输入框附初始值
        filed.text = @"文字";
//      2. textcolor
        filed.textColor = [UIColor blueColor];
//        3. boredStyle 边缘样式默认是无
//        边缘样式UITextBorderStyle
//        UITextBorderStyleNone, 无
//        UITextBorderStyleLine, 有边缘线
//        UITextBorderStyleBezel, 加深边缘线
//        UITextBorderStyleRoundedRect 边缘带圆角的
        filed.borderStyle = UITextBorderStyleRoundedRect;
//       4. placeholder 占位符
//        当text不为空时 placeholder是不显示的
//       5. clearsOnBeginEditing
      filed.placeholder = @"请输入您的爱好";
//        command + k 键盘
        filed.clearsOnBeginEditing = YES;
        
//      签订方法协议
        
        filed.delegate = self;
        [_window addSubview:filed];
    }
#endif
    
    return YES;
}

- (void)buttonWithChilk:(UIButton *)btn {
//    结束编辑
    [_window endEditing:YES];
    NSLog(@"张合蔚别点击了");
}
//实现协议方法

//将要开始之前能否编辑 (开始编辑)
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    returnYES 说明可以编辑
//    returnNO 说明不可以编辑
    return YES;
}

// 已经开始编辑时 调用的方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"编辑方法");
}
//是否可以结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}
// 已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"完成编辑");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    处理点击return，需要进行的操作
//    回收键盘
    [textField endEditing:YES];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
